package com.xsprite.managesys.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xsprite.managesys.bean.ItemLog;
import com.xsprite.managesys.bean.QueryEmailInfo;
import com.xsprite.managesys.bean.QueryServer;
import com.xsprite.managesys.service.ServersDatabaseManager;

/**
 * Servlet implementation class QueryEmail
 */
public class QueryEmailServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest req;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.service(req, resp);
		this.req = req;
		if (isParameterNull("playerName") || isParameterNull("serverId") || isParameterNull("date")) {
			return;
		}
		String playerName = queryParam("playerName");
		String serverId = queryParam("serverId");
		String date = queryParam("date");
		String userid = req.getSession().getAttribute("userid").toString();
		try {
			String toQueryTable = getQueryTable(date);
			QueryServer server = ServersDatabaseManager.getInstance().getServerById(Integer.parseInt(serverId));
			List<ItemLog> itemLogs = ServersDatabaseManager.getInstance().queryEmail(server.getGameDBId(),
					server.getLogDBId(), playerName, toQueryTable, userid);
			if (itemLogs != null && itemLogs.size() > 0) {
				req.setAttribute("itemLogs", itemLogs);
			} else {
				req.setAttribute("errorInfo", "沒有結果");
			}
			injectToClientInfos(serverId, playerName, date);
			forwardTo("/view/queryEmail.jsp");
		} catch (Exception ex) {
			injectToClientInfos(serverId, playerName, date);
			forwardTo("/view/queryEmail.jsp");
		}
	}

	private void injectToClientInfos(String serverId, String playerName, String date) {
		QueryEmailInfo queryEmailInfo = new QueryEmailInfo();
		queryEmailInfo.setServerId(Integer.parseInt(serverId));
		queryEmailInfo.setPlayerName(playerName);
		queryEmailInfo.setDate(date);
		req.setAttribute("queryEmailInfo", queryEmailInfo);
		ServersDatabaseManager manager = ServersDatabaseManager.getInstance();
		List<QueryServer> servers = manager.getServersLsit();
		req.setAttribute("queryemailServers", servers);
	}

	private String getQueryTable(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
		Date d = sdf.parse(date);
		int year = d.getYear();
		int month = d.getMonth();
		int day = d.getDate();
		int hour = d.getHours();
		String resultHour = "00";
		if (hour > 9) {
			resultHour = String.valueOf(hour);
		} else {
			resultHour = "0" + hour;
		}
		String toQueryTable = "ItemLog" + year % 100 + ((month > 9) ? month : ("0" + month)) + ""
				+ ((day > 9) ? day : ("0" + day));
		// String toQueryTable = "ItemLog" + year % 100 + ((month >
		// 9)?month:("0" + month)) + "" +((day > 9)?day:("0" + day)) + "" +
		// resultHour;
		return toQueryTable;
	}
}
